package main

import (
	"context"
	amqp "github.com/rabbitmq/amqp091-go"
	"log"
	"os"
	"time"
	"zhongxc.cc/go-common/errs"
	"zhongxc.cc/go-common/mq"
	"zhongxc.cc/go-common/utils"
)

func main() {
	conn := mq.NewRabbitMqConnection()
	channel, err := conn.Channel()
	errs.FailOnError(err, "Failed to open a channel")
	defer func(conn *amqp.Connection) {
		_ = conn.Close()
	}(conn)
	defer func(channel *amqp.Channel) {
		_ = channel.Close()
	}(channel)

	// 创建交换机
	err = channel.ExchangeDeclare(
		"logs",   // 使用命名的交换器
		"fanout", // 交换器类型
		true,
		false,
		false,
		false,
		nil,
	)
	errs.FailOnError(err, "Failed to declare an exchange")

	ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancelFunc()

	body := utils.BodyFrom(os.Args)
	err = channel.PublishWithContext(ctx,
		"logs", // exchange
		"",     // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(body),
		},
	)
	errs.FailOnError(err, "Failed to publish a message")
	log.Printf(" [x] Sent %s\n", body)
}

/**
## 列出服务器上的交换器
sudo rabbitmqctl list_exchanges
## 列出绑定关系
sudo rabbitmqctl list_bindings
*/
